import { reqGoodsList } from "../../../../../api/goods";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [], // 商品列表数据
    total: 0,
    isFinish: false, // 判断数据是否加载完毕
    // 商品列表请求参数
    requestData: {
      page: 1,
      limit: 10,
      category1Id: "",
      category2Id: "",
    },
  },

  // 获取商品列表数据
  async getGoodsList() {
    this.data.isFinish = true; // 表示请求正在发送中
    const { data } = await reqGoodsList(this.data.requestData);
    this.data.isFinish = false;
    console.log("获取商品列表数据=》", data);
    this.setData({
      goodsList:
        this.data.page === 1
          ? data.data.records
          : this.data.goodsList.concat(data.data.records),
      total: data.data.total,
    });
  },

  // 监听页面的上拉加载
  onReachBottom() {
    if (this.data.isFinish) return;
    const { page } = this.data.requestData;
    if (this.data.goodsList.length < this.data.total) {
      this.setData({
        requestData: { ...this.data.requestData, page: page + 1 },
      });
      this.getGoodsList();
    }
  },

  // 监听页面的下拉刷新操作
  onPullDownRefresh() {
    this.setData({
      goodsList: [],
      total: 0,
      isFinish: false,
      requestData: { ...this.data.requestData, page: 1 },
    });
    this.getGoodsList();
    wx.stopPullDownRefresh();
  },

  // 转发功能
  onShareAppMessage(){
    return {
      title:"只为你怦然心动",
      path:'/pages/index/index',
      imageUrl:'../../../../../assets/images/love.jpg'
    }
  },

  // 分享朋友圈
  onShareTimeline() {
   return {
     title:"只为你怦然心动",
     path:'/pages/index/index',
     imageUrl:'../../../../../assets/images/love.jpg'
   }
  },

  onLoad(options) {
    Object.assign(this.data.requestData, options);

    this.getGoodsList();
  },
});
